package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.MarketCommodity;
import com.woniuxy.common.entity.MarketMessage;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.MarketCommodityMapper;
import com.woniuxy.common.mapper.MarketMessageMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.MarketMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MarketMessageServiceImpl implements MarketMessageService {
    @Autowired
    MarketMessageMapper marketMessageMapper;

    @Autowired
    MarketCommodityMapper marketCommodityMapper;

    @Autowired
    CenterUserMapper centerUserMapper;

    @Override
    public List<MarketMessage> findAllMessagesBySendId(String sendUserId) {
        List<MarketMessage> marketMessages = marketMessageMapper.selectAllMessagesBySendId(sendUserId);
        for (MarketMessage marketMessage:marketMessages) {
            MarketCommodity commodity = marketCommodityMapper.selectByPrimaryKey(marketMessage.getCommodityId());
            CenterUser receiveUser = centerUserMapper.selectByPrimaryKey(marketMessage.getReceiveuserId());
            marketMessage.setCommodity(commodity);
            marketMessage.setReceiveUser(receiveUser);
        }
        return marketMessages;
    }

    @Override
    public MarketMessage selectMessageByInfo(String loginuserid, String commodityid, String userid) {
        MarketMessage marketMessage = marketMessageMapper.selectMessageByInfo(loginuserid, commodityid, userid);
        return marketMessage;
    }

    @Transactional
    @Override
    public int insertMessageByInfo(String loginuserid, String commodityid, String userid) {
        //插入消息的时候插入两条，一条发送方为我，另一条发送方为对方
        MarketMessage marketMyMessage=new MarketMessage();
        marketMyMessage.setId(KeyUtil.generateKey());
        marketMyMessage.setReceiveuserId(userid);
        marketMyMessage.setSenduserId(loginuserid);
        marketMyMessage.setCommodityId(commodityid);
        marketMyMessage.setDate(new Date());
        //对方为发送方显示的消息
        MarketMessage marketUserMessage=new MarketMessage();
        marketUserMessage.setId(KeyUtil.generateKey());
        marketUserMessage.setReceiveuserId(loginuserid);
        marketUserMessage.setSenduserId(userid);
        marketUserMessage.setCommodityId(commodityid);
        marketMyMessage.setDate(new Date());
        int insert1 = marketMessageMapper.insert(marketMyMessage);
        int insert2 = marketMessageMapper.insert(marketUserMessage);
        if(insert1!=0){
            if(insert2!=0){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int updateDate(String id,Date date) {
        int i = marketMessageMapper.updateDate(id, date);
        return i;
    }
}
